﻿using System.Web;
using ZLDC.Common.Estate;
using System;

namespace ZLDC.Estate
{
    public class CHandlerAlert
    {
        public static void ChooseMethod()
        {

            string act = _Request.GetString("act", "");

            switch (act)
            {
                #region 获取提醒
                case "alertlist":
                    AlertList();
                    break;
                case "todayalert":
                    TodayAlert(); break;
                case "housingalert": HousingAlert();
                    break;
                case "customeralert": CustomerAlert();
                    break;
                case "KeyOrclusiveList": KeyOrclusiveList();
                    break;
                case "SelectCommendHousingList": SelectCommendHousingList();
                    break;
                #endregion

            }
        }

        private static void CustomerAlert()
        {
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID);
            string subsidiary = _Utility.GetInfoBySession().m_sFgs;
            string station = _Utility.GetInfoBySession().m_sStation;
            string deptChildList = _Utility.GetInfoBySession().m_sChildrenDeptList;
            string json = string.Empty;
            if (!_Utility.GetWebConfigSetting("OutOfSalesManagerStation").Contains(station))
            {
                string condition = " where isSelf=0 and dltflag=0 and [status]=0";
                if (_Utility.GetWebConfigSetting("SalesManagerStation").Contains(station))
                    condition += " and ownerdeptid in (" + deptChildList + ")";
                else
                    condition += " and ownerid =" + userid;
                condition += " and datediff(day,followUpDate,getdate()) > (select top 1 itemValue-(select top 1 itemValue from dbo.t_system_option where item='私客待转公客提前提醒天数' and subsidiary in ('总公司', '" + subsidiary + "') order by ID desc) from dbo.t_system_option where item='私客转公客天数' and subsidiary in ('总公司', '" + subsidiary + "') order by ID desc) ";


                json = ZLDC.Topo.BLL.Estate.AlertBLL.GetCustomerAlert(condition);
            }
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void HousingAlert()
        {
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID);
            int deptid = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID);
            string subsidiary = _Utility.GetInfoBySession().m_sFgs;
            string station = _Utility.GetInfoBySession().m_sStation;
            string deptChildList = _Utility.GetInfoBySession().m_sChildrenDeptList;

            string json = string.Empty;
            if (!_Utility.GetWebConfigSetting("OutOfSalesManagerStation").Contains(station))
            {
                string condition = " where status='1' and dltflag=0";
                if (_Utility.GetWebConfigSetting("SalesManagerStation").Contains(station))
                    condition += " and markUDeptid in (" + deptChildList + ")";
                else
                    condition += " and markUid =" + userid;

                json = ZLDC.Topo.BLL.Estate.AlertBLL.GetHousingAlert(condition, subsidiary);
            }
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        private static void TodayAlert()
        {
            int pagesize = _Request.GetInt("pagesize", 0);
            int page = _Request.GetInt("page", 0);
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            int deptid = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
            string station = _Utility.GetInfoBySession().m_sStation;

            string json = ZLDC.Topo.BLL.Estate.AlertBLL.GetTodayAlert(page, pagesize, userid, deptid);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }


        private static void BatchUpdateAlert()
        {
            string[] TypeId = _Utility.GetStringBySplit(_Request.GetString("typeid"), '|');
            string[] IDList = _Utility.GetStringBySplit(_Request.GetString("id"), '|');

            int deptid = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID);
            string dept = _Utility.GetInfoBySession().m_sDeptName;
            int duserid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID);
            string duser = _Utility.GetInfoBySession().m_sUserName;

            string json = "";
            int id = 0;
            for (int i = 0; i < TypeId.Length; i++)
            {

                if (TypeId[i] == "1")
                {
                    id = Convert.ToInt32(IDList[i]);

                    if (ZLDC.Topo.BLL.Estate.CustomerBLL.p_CustomerAlert_update(id, 1, deptid, dept, duserid, duser) > 0)
                        json = "处理成功";

                    else
                        json = "处理失败";


                }
                else if (TypeId[i] == "2")
                {
                    id = Convert.ToInt32(IDList[i]);
                    json = ZLDC.Topo.BLL.Estate.HousingAlertBLL.p_housingAlert_update(id, 1, deptid, dept, duserid, duser);

                }
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();


        }

        #region 获取提醒

        private static void AlertList()
        {
            int pagesize = _Request.GetInt("pagesize", 0);
            int page = _Request.GetInt("page", 0);
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID);
            int deptid = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID);


            string json = ZLDC.Topo.BLL.Estate.AlertBLL.GetAllAlert(page, pagesize, userid, deptid);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        #endregion


        #region 钥匙，独家，提醒

        private static void KeyOrclusiveList()
        {
            string userid = _Utility.GetInfoBySession().m_sUserID;

            string json = ZLDC.Topo.BLL.Estate.HousingBLL.P_t_keyorExclusive_list(userid);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();

        }

        private static void SelectCommendHousingList()
        {
            string json = ZLDC.Topo.BLL.Estate.HousingBLL.p_CommendHousing_list();
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();

        }
        #endregion
    }
}
